package thread.concurrence;

import java.util.concurrent.atomic.AtomicInteger;

import thread.Customer;
import thread.Ticket;

public class ConcurrentTicket extends Ticket {
    /** 可以用原子方式更新的int值 */
    private static AtomicInteger count = new AtomicInteger();

    public static Ticket sellOne(Customer buyer) {
        if (count.intValue() > 0) {
            count.decrementAndGet();
            Ticket ticket = new ConcurrentTicket(buyer);
            System.out.println("售票处已卖出一张票，购买此票的顾客是" + ticket.getBuyer().getName());
            return ticket;
        }
        return null;
    }
    
    public static void addCount(int extraCount) {
        count.addAndGet(extraCount > 0 ? extraCount : 0);
    }
    
    private ConcurrentTicket(Customer buyer) {
        super(buyer);
    }
}
